package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.R;
import com.itheima.reggie.pojo.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import org.apache.catalina.util.LifecycleMBeanBase;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 首页面的购物车数据,通过当前用户的id查询除购物车中有什么
     * @return
     */
    @GetMapping("/list")
   public R<List<ShoppingCart>> list(){
    Long user_Id = BaseContext.getCurrentId();
    LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
    shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,user_Id);
    List<ShoppingCart> list = shoppingCartService.list(shoppingCartLambdaQueryWrapper);
    return R.success(list);
}

    /**
     * 根据传入的购物车数据进行添加先对当前用户进行查询,再通过判断传进来的参数的dish_id是否为空,不为空就代表添加的是菜品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        Long user_Id = BaseContext.getCurrentId();
        shoppingCart.setUserId(user_Id);
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,user_Id);
        if (shoppingCart.getDishId()!=null){//添加的是菜品
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //查询除当前添加操作的对象
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (shoppingCartOne!=null){
            shoppingCartOne.setNumber(shoppingCartOne.getNumber()+1);
            shoppingCartService.updateById(shoppingCartOne);
        }else {
            //给传进来的shoppingCart  set值然后再赋给shoppingCartOne因为最后return的是shoppingCartOne
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            shoppingCartOne=shoppingCart;
        shoppingCartService.save(shoppingCartOne);
        }
        return R.success(shoppingCartOne);
    }

    /**
     * 通过传入的dish_id或者setmeal_id来进行减少操作,要先进行number-1的操作,当<=0时直接就删除购物车,如果不是<=0,就进行修改操作,因为前面number-1
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("sub")
    public R<ShoppingCart> sub(@RequestBody ShoppingCart shoppingCart){
        LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
        shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        if (shoppingCart.getDishId()!=null){//想减少的是菜品的数量
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getDishId,shoppingCart.getDishId());
        }else {//想减少的是套餐的数量
            shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }//类似添加的操作,都是通过一次一次的判断来看是操作菜品还是套餐
        //上面只走一条
        ShoppingCart shoppingCartOne = shoppingCartService.getOne(shoppingCartLambdaQueryWrapper);
        if (shoppingCartOne==null){
            throw new CustomException("购物车为空,无法进行减少操作");
        }
        Integer number = shoppingCartOne.getNumber();//当前的数量
        shoppingCartOne.setNumber(number-1);
        if (number-1<=0){//上面已经减少了.这里代表如果执行了减少操作后为数量0了就删除购物车中的这个对象数据
            shoppingCartService.removeById(shoppingCartOne);
        }else {
            shoppingCartService.updateById(shoppingCartOne);//否则上面已经减少了number进行修改操作.
        }
         return R.success(shoppingCartOne);
    }
         @DeleteMapping("/clean")
    public R clean(){
             LambdaQueryWrapper<ShoppingCart> shoppingCartLambdaQueryWrapper = new LambdaQueryWrapper<>();
             shoppingCartLambdaQueryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
            shoppingCartService.remove(shoppingCartLambdaQueryWrapper);
            return R.success(null);
         }

}
